package pixelradar.gui;

import com.pixelmonmod.pixelmon.enums.EnumGrowth;
import com.pixelmonmod.pixelmon.enums.forms.EnumSpecial;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.resources.I18n;
import org.lwjgl.input.Keyboard;
import pixelradar.util.Logging;
import pixelradar.util.VersionCheck;

import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RadarGui extends GuiScreen {
    private static final String GROWTH_LABEL = "mw.gui.search.filter.growth";
    private static final String FORM_LABEL = "mw.gui.search.filter.form";
    private static final int SEARCH_WIDTH = 220;
    private static final String OPTION_TITLE = "mw.gui.search.settings";
    private static final String UPDATE_AVAIL = "mw.gui.updatechecker.avail";
    public static String searchString = "";
    private static GuiTextField search = null;
    private static GuiButton options = null;
    private static GuiButton updateButton = null;
    private static List<EnumGrowth> growthList;
    private static List<EnumSpecial> formList;
    private static int growthIndex = 0;
    private static int formIndex = 0;

    public static EnumGrowth getGrowth() {
        return growthList == null ? null : growthList.get(growthIndex);
    }

    public static EnumSpecial getForm() {
        return formList == null ? null : formList.get(formIndex);
    }

    public static String getGrowthLabel() {
        return getGrowth() == null ? I18n.format("mw.gui.search.filter.growth.off") : getGrowth().getLocalizedName();
    }

    public static String getFormLabel() {
        return getForm() == null ? I18n.format("mw.gui.search.filter.form.off") : getForm().getLocalizedName();
    }

    public void initGui() {
        super.initGui();
        search = new GuiTextField(0, this.fontRenderer, this.width / 2 - 110, 40, 220, 18);
        search.setMaxStringLength(255);
        search.setText(searchString);
        search.setFocused(true);
        Keyboard.enableRepeatEvents(true);
        growthList = new ArrayList<>();
        growthList.add(null);
        growthList.addAll(Arrays.asList(EnumGrowth.values()));
        formList = new ArrayList<>();
        formList.add(null);
        formList.addAll(Arrays.asList(EnumSpecial.values()));
        this.buttonList.add(new GuiButton(100, this.width / 2 - 110 + 16, 130, 15, 20, "<"));
        this.buttonList.add(new GuiButton(101, this.width / 2 - 110 + 40 + this.fontRenderer.getStringWidth(I18n.format("mw.gui.search.filter.growth")) * 2, 130, 15, 20, ">"));
        this.buttonList.add(new GuiButton(200, this.width / 2 - 110 + 16, 90, 15, 20, "<"));
        this.buttonList.add(new GuiButton(201, this.width / 2 - 110 + 40 + this.fontRenderer.getStringWidth(I18n.format("mw.gui.search.filter.form")) * 2, 90, 15, 20, ">"));
        this.buttonList.add(options = new GuiButton(1, this.width / 2 - this.fontRenderer.getStringWidth(I18n.format("mw.gui.search.settings")) / 2, 170, this.fontRenderer.getStringWidth(I18n.format("mw.gui.search.settings")) + 15, 20, I18n.format("mw.gui.search.settings")));
        if (!VersionCheck.isLatestVersion()) {
            this.buttonList.add(updateButton = new GuiButton(2, 2, 2, this.fontRenderer.getStringWidth(I18n.format("mw.gui.updatechecker.avail") + ": " + VersionCheck.getLatestVersion()) + 15, 20, I18n.format("mw.gui.updatechecker.avail") + VersionCheck.getLatestVersion()));
        }

    }

    public void onGuiClosed() {
        Keyboard.enableRepeatEvents(false);
    }

    public void updateScreen() {
        super.updateScreen();
        searchString = search.getText();
    }

    public void drawScreen(int mouseX, int mouseY, float f) {
        this.drawDefaultBackground();
        this.drawCenteredString(this.fontRenderer, I18n.format("mw.gui.search.title"), this.width / 2, 30, Color.WHITE.getRGB());
        GlStateManager.scale(2.0F, 2.0F, 1.0F);
        this.drawString(this.fontRenderer, I18n.format("mw.gui.search.filter.growth"), this.width / 4 - 55 + 19, 67, Color.WHITE.getRGB());
        this.drawString(this.fontRenderer, I18n.format("mw.gui.search.filter.form"), this.width / 4 - 55 + 19, 47, Color.WHITE.getRGB());
        GlStateManager.scale(0.5D, 0.5D, 1.0D);
        this.drawString(this.fontRenderer, getGrowthLabel(), this.width / 2 - 110 + 37, 153, Color.ORANGE.getRGB());
        this.drawString(this.fontRenderer, getFormLabel(), this.width / 2 - 110 + 37, 113, Color.ORANGE.getRGB());
        if (VersionCheck.isLatestVersion()) {
            this.drawString(this.fontRenderer, "PixelRadar v5.4 for Pixelmon 8.3.8!", 2, 2, Color.GRAY.getRGB());
        }

        search.drawTextBox();
        super.drawScreen(mouseX, mouseY, f);
    }

    protected void keyTyped(char c, int i) throws IOException {
        if (i != 28 && i != 43) {
            super.keyTyped(c, i);
            search.textboxKeyTyped(c, i);
        } else {
            this.mc.displayGuiScreen(null);
        }
    }

    public boolean doesGuiPauseGame() {
        return true;
    }

    protected void actionPerformed(GuiButton button) throws IOException {
        if (button.id == 100) {
            if (growthIndex != 0) {
                --growthIndex;
            } else {
                growthIndex = growthList.size() - 1;
            }
        }

        if (button.id == 101) {
            if (growthIndex != growthList.size() - 1) {
                ++growthIndex;
            } else {
                growthIndex = 0;
            }
        }

        if (button.id == 200) {
            if (formIndex != 0) {
                --formIndex;
            } else {
                formIndex = formList.size() - 1;
            }
        }

        if (button.id == 201) {
            if (formIndex != formList.size() - 1) {
                ++formIndex;
            } else {
                formIndex = 0;
            }
        } else if (button == options) {
            try {
                GuiScreen newScreen = ModGuiConfig.class.getConstructor(GuiScreen.class).newInstance(this);
                this.mc.displayGuiScreen(newScreen);
            } catch (Exception var4) {
                Logging.logError("There was a critical issue trying to build the pixelradar.config GUI for %s", "pixelradar");
            }
        } else if (button == updateButton && Desktop.isDesktopSupported()) {
            try {
                Desktop.getDesktop().browse(new URI(VersionCheck.getUpdateURL()));
            } catch (URISyntaxException var3) {
                var3.printStackTrace();
            }
        }

    }

    private String getSelectionString(ButtonRotator buttonRotator) {
        Enum<?> selected = buttonRotator.getSelected();
        return selected == null ? buttonRotator.getDefaultLabel() : selected.toString();
    }
}
